package main

import (
	"china_region_spider/tools"
	"github.com/gocolly/colly"
)

type srcAddress struct {
	Name string
	Href string
}

var basicURL = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2021/"
var err error

func main() {
	// 获取日志记录对象
	mLogger, mCtx := tools.GetLogger()

	var provinceList []srcAddress
	var cityList []srcAddress
	var countyList []srcAddress

	mCollector := tools.GetCollector()

	// 省级地址获取
	mCollector.OnHTML("tr .provincetr", func(ele *colly.HTMLElement) {
		ele.ForEach("td", func(_ int, element *colly.HTMLElement) {
			addrInfo := tools.AddressInfo{
				AddressName:  element.ChildText("a"),
				ParentId:     0,
				AddressLevel: 1,
			}

			// 如果记录不存在，将记录持久化
			if !tools.IsExistRecord(&addrInfo) {
				tools.AddRecord(&addrInfo)

				p := srcAddress{
					Name: element.ChildText("a"),
					Href: element.ChildAttr("a", "href"),
				}

				provinceList = append(provinceList, p)
			}
		})
	})

	err = mCollector.Visit(basicURL + "index.html")
	if err != nil {
		mLogger.Info(mCtx, "网络访问失败：", basicURL+"index.html")
	}

	mCollector.Wait()

	// 市级地址信息获取
	for _, province := range provinceList {
		mCollector.OnHTML("tr .citytr", func(ele *colly.HTMLElement) {
			// 从数据库中查找上级地址ID
			addressName := province.Name
			parentId := tools.GetParentId(addressName, 1)

			addrInfo := tools.AddressInfo{
				AddressName:  ele.ChildText("td:last-child"),
				ParentId:     parentId,
				AddressLevel: 2,
			}

			// 如果记录不存在，将记录持久化
			if !tools.IsExistRecord(&addrInfo) {
				tools.AddRecord(&addrInfo)

				p := srcAddress{
					Name: ele.ChildText("td:last-child"),
					Href: ele.ChildAttr("td a", "href"),
				}

				cityList = append(cityList, p)
			}
		})

		err = mCollector.Visit(basicURL + province.Href)
		if err != nil {
			mLogger.Info(mCtx, "网络访问失败：", basicURL+province.Href)
		}
	}

	mCollector.Wait()

	// 县级地址信息获取
	for _, city := range cityList {
		mCollector.OnHTML("tr .countytr", func(ele *colly.HTMLElement) {
			// 从数据库中查找上级地址ID
			addressName := city.Name
			parentId := tools.GetParentId(addressName, 2)

			addrInfo := tools.AddressInfo{
				AddressName:  ele.ChildText("td:last-child"),
				ParentId:     parentId,
				AddressLevel: 2,
			}

			// 如果记录不存在，将记录持久化
			if !tools.IsExistRecord(&addrInfo) {
				tools.AddRecord(&addrInfo)

				p := srcAddress{
					Name: ele.ChildText("td:last-child"),
					Href: ele.ChildAttr("td a", "href"),
				}

				countyList = append(countyList, p)
			}
		})

		err = mCollector.Visit(basicURL + city.Href)
		if err != nil {
			mLogger.Info(mCtx, "网络访问失败：", basicURL+city.Href)
		}
	}
}
